package com.easylinkin.linkappapi.airconditioner.controller;

import com.easylinkin.linkappapi.airconditioner.entity.AirHostDevice;
import com.easylinkin.linkappapi.airconditioner.service.AirHostDeviceService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

/**
 * @program: linkapp-group
 * @description: 主机设备
 * @author: chenkaixuan
 * @create: 2021-05-08 11:23
 */
@RestController
@RequestMapping("/airconditioner/airHostDevice")
public class AirHostDeviceController {
    @Autowired
    AirHostDeviceService airHostDeviceService;

    /**
     * 查询所有列表
     * @return
     */
    @GetMapping("getAll")
    public RestMessage getAll(){
        return RestBuilders.successBuilder(airHostDeviceService.getAll()).build();
    }

    /***
     * 批量添加
     * @return
     */
    @PostMapping("insertBatch")
    public RestMessage insertBatch(@RequestBody List<AirHostDevice> airHostDeviceList){
        airHostDeviceService.batchInsert(airHostDeviceList);
        return RestBuilders.successBuilder().build();
    }

    /***
     * 下载导入模板
     * @param request
     * @param response
     */
    @GetMapping("getImportTemplate")
    public void getImportTemplate(HttpServletRequest request, HttpServletResponse response) {
        airHostDeviceService.getImportTemplate(request, response);
    }

    @PostMapping("importData")
    public RestMessage importData(@RequestParam(value = "file") MultipartFile file) {
        Assert.notNull(file, "文件不能为空");
        Set errors = airHostDeviceService.importData(file);
        if(errors.isEmpty()) {
            return RestBuilders.successBuilder().data("导入成功").build();
        }else {
            String errorMsg = "";
            for(Object obj : errors) {
                errorMsg = errorMsg +"第" + obj.toString() + "行  ";
            }
            errorMsg = errorMsg + "数据错误,请核对后再提交。";
            return RestBuilders.successBuilder().data(errorMsg).build();
        }
    }

    @PostMapping("deleteByIds")
    public RestMessage deleteBatch(@RequestBody AirHostDevice airHostDevice){
        airHostDeviceService.removeIds(airHostDevice);
        return RestBuilders.successBuilder().build();
    }

    /***
     * 修改
     * @return
     */
    @PostMapping("updateById")
    public RestMessage updateAirHostDeviceById(@RequestBody AirHostDevice airHostDevice){
        airHostDeviceService.updateAirHostDeviceById(airHostDevice);
        return RestBuilders.successBuilder().build();
    }
}
